using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


// 观察者模式：一对多的依赖关系，多个观察者对象同时监听一个对象，当对象发生变化时，会通知多有观察者对象，使它们能够自动更新自己

/// <summary>
/// 事件管理器 
/// </summary>
public class EventManager :Singleton<EventManager>
{
    public delegate void EventMsg(object obj);
    public delegate void MyEvent();

    private  Dictionary<string, MyEvent> eventDic = new Dictionary<string, MyEvent>();
    private Dictionary<string, EventMsg> msgDic = new Dictionary<string, EventMsg>();

    #region MyEvent
    public void AddListen(string key, MyEvent eEvent)
    {
        if (eEvent != null)
        {
            if (eventDic.ContainsKey(key))
            {
                eventDic[key] += eEvent;
            }
            else
            {
                eventDic[key] = eEvent;
            }
        }
    }

    public void RemoveListen(string key, MyEvent eEvent)
    {
        if (eventDic.ContainsKey(key))
        {
            eventDic[key] = eventDic[key] - eEvent;
        }
    }

    public void Dispatch(string key)
    {
        if (eventDic.ContainsKey(key))
        {
            eventDic[key]();
        }
    }


    #endregion


    #region EventMsg

    public void AddListen(string key, EventMsg eEvent)
    {
        if (eEvent != null)
        {
            if (msgDic.ContainsKey(key))
            {
                msgDic[key] += eEvent;
            }
            else
            {
                msgDic[key] = eEvent;
            }
        }
    }

    public void RemoveListen(string key, EventMsg eEvent)
    {
        if (msgDic.ContainsKey(key))
        {
            msgDic[key] = msgDic[key] - eEvent;
        }
    }

    public void Dispatch(string key, object obj)
    {
        if (eventDic.ContainsKey(key))
        {
            msgDic[key](obj);
        }
    }


    #endregion


}
